package com.netposa.datawarehouse.aggregator.web.rest;

import com.alibaba.fastjson.JSONObject;
import com.netposa.appwebres.WebResCallback;
import com.netposa.appwebres.common.WebResCriteria;
import com.netposa.appwebres.common.WebResInfo;
import com.netposa.appwebres.exception.WebResException;
import com.netposa.datawarehouse.aggregator.portrait.model.PortraitCondition;
import com.netposa.datawarehouse.aggregator.portrait.model.PortraitSearchCondition;
import com.netposa.datawarehouse.aggregator.service.ImgSearchService;
import com.netposa.datawarehouse.aggregator.web.model.ExcludeCondition;
import com.netposa.datawarehouse.aggregator.web.vo.FeatureDetectParam;
import com.netposa.datawarehouse.aggregator.web.vo.FeatureSearchParam;
import com.netposa.datawarehouse.aggregator.web.vo.FileSearchParam;
import com.netposa.imageserver.client.exception.ImageServerException;
import com.netposa.vdt.pccclient.exception.PccHandleException;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/img_search"})
public class ImgSearchController {
   private static final Logger log = LoggerFactory.getLogger(ImgSearchController.class);
   @Autowired
   private ImgSearchService imgSearchService;

   @PostMapping({"/full_search"})
   public WebResInfo fullSearch(final FeatureSearchParam featureSearchParam) {
      return (new WebResCallback() {
         public void execute(WebResCriteria criteria, Object... params) {
            try {
               criteria.addSingleResult(ImgSearchController.this.imgSearchService.fullSearch(featureSearchParam));
            } catch (WebResException var4) {
               throw var4;
            } catch (Exception var5) {
               ImgSearchController.log.error(var5.getMessage(), var5);
               throw new WebResException("499", "人脸服务异常，请稍后重试!");
            }
         }
      }).sendRequest(new Object[0]);
   }

   @PostMapping({"/fullSearch/file"})
   public WebResInfo fullSearchForFile(@RequestBody final FileSearchParam fileSearchParam) {
      return (new WebResCallback() {
         public void execute(WebResCriteria criteria, Object... params) {
            try {
               criteria.addSingleResult(ImgSearchController.this.imgSearchService.fullSearchImg(fileSearchParam));
            } catch (WebResException var4) {
               throw var4;
            } catch (Exception var5) {
               ImgSearchController.log.error(var5.getMessage(), var5);
               throw new WebResException("499", new Object[]{"服务异常"});
            }
         }
      }).sendRequest(new Object[0]);
   }

   @PostMapping({"/feature_detect_new"})
   public WebResInfo featureDetectNew(final FeatureDetectParam featureDetectParam) {
      return (new WebResCallback() {
         public void execute(WebResCriteria criteria, Object... params) {
            try {
               criteria.addSingleResult(ImgSearchController.this.imgSearchService.featureDetectNew(featureDetectParam));
            } catch (PccHandleException var4) {
               throw new WebResException("499", new Object[]{var4.getMessage()});
            } catch (WebResException var5) {
               throw var5;
            } catch (ImageServerException var6) {
               throw new WebResException("499", new Object[]{"上传图片异常"});
            } catch (TException var7) {
               throw new WebResException("499", new Object[]{"大数据平台连接异常"});
            } catch (Exception var8) {
               throw new WebResException("500", new Object[]{"参数异常"});
            }
         }
      }).sendRequest(new Object[0]);
   }

   @PostMapping({"/detect_all_feature"})
   public WebResInfo detectAllFeature(final FeatureDetectParam featureDetectParam) {
      return (new WebResCallback() {
         public void execute(WebResCriteria criteria, Object... params) {
            try {
               criteria.addSingleResult(ImgSearchController.this.imgSearchService.detectAllFeature(featureDetectParam));
            } catch (PccHandleException var4) {
               throw new WebResException("499", new Object[]{var4.getMessage()});
            } catch (WebResException var5) {
               throw var5;
            } catch (ImageServerException var6) {
               ImgSearchController.log.error("上传图片异常", var6);
               throw new WebResException("499", new Object[]{"上传图片异常"});
            } catch (TException var7) {
               throw new WebResException("499", new Object[]{"大数据平台连接异常"});
            } catch (Exception var8) {
               throw new WebResException("500", new Object[]{"参数异常"});
            }
         }
      }).sendRequest(new Object[0]);
   }

   @PostMapping({"/detect_all_feature_1400"})
   public WebResInfo detectAllFeature1400(@RequestBody final FeatureDetectParam featureDetectParam) {
      return (new WebResCallback() {
         public void execute(WebResCriteria criteria, Object... params) {
            try {
               criteria.addSingleResult(ImgSearchController.this.imgSearchService.detectAllFeature(featureDetectParam));
            } catch (PccHandleException var4) {
               throw new WebResException("499", new Object[]{var4.getMessage()});
            } catch (WebResException var5) {
               throw var5;
            } catch (ImageServerException var6) {
               ImgSearchController.log.error("上传图片异常", var6);
               throw new WebResException("499", new Object[]{"上传图片异常"});
            } catch (TException var7) {
               throw new WebResException("499", new Object[]{"大数据平台连接异常"});
            } catch (Exception var8) {
               throw new WebResException("500", new Object[]{"参数异常"});
            }
         }
      }).sendRequest(new Object[0]);
   }

   @PostMapping({"/full_search_1400"})
   public WebResInfo fullSearch1400(@RequestBody final FeatureSearchParam featureSearchParam) {
      return (new WebResCallback() {
         public void execute(WebResCriteria criteria, Object... params) {
            try {
               criteria.addSingleResult(ImgSearchController.this.imgSearchService.fullSearch(featureSearchParam));
            } catch (WebResException var4) {
               throw var4;
            } catch (Exception var5) {
               ImgSearchController.log.error(var5.getMessage(), var5);
               throw new WebResException("499", new Object[]{"人脸服务异常，请稍后重试!"});
            }
         }
      }).sendRequest(new Object[0]);
   }

   @PostMapping({"/exclude_unchoiced_feature"})
   public WebResInfo excludeUnchoicedFeature(@RequestBody final ExcludeCondition excludeCondition) {
      return (new WebResCallback() {
         public void execute(WebResCriteria criteria, Object... params) {
            ImgSearchController.log.info("input sessionKey={}", excludeCondition.getSessionKey());
            ImgSearchController.log.info("input includeDataKey={}", excludeCondition.getIncludeDataKey());
            List excludedDataKeyList = new ArrayList();
            if (StringUtils.isNotBlank(excludeCondition.getExcludedDataKeys())) {
               excludedDataKeyList = Arrays.asList(excludeCondition.getExcludedDataKeys().split(","));
            }

            Iterator var4 = ((List)excludedDataKeyList).iterator();

            while(var4.hasNext()) {
               String dataKey = (String)var4.next();
               ImgSearchController.log.info("input dataKey={}", dataKey);
            }

            criteria.addSingleResult(ImgSearchController.this.imgSearchService.excludeUnselectedFeatures(excludeCondition));
         }
      }).sendRequest(new Object[0]);
   }

   @GetMapping({"/clean_session"})
   public WebResInfo cleanSession(final String sessionkey) {
      return (new WebResCallback() {
         public void execute(WebResCriteria criteria, Object... params) {
            ImgSearchController.this.imgSearchService.cleanSession(sessionkey);
         }
      }).sendRequest(new Object[0]);
   }

   @GetMapping({"/clean_detect"})
   public WebResInfo cleanDetect(final String sessionKey, final String dataKey) {
      return (new WebResCallback() {
         public void execute(WebResCriteria criteria, Object... params) {
            ImgSearchController.this.imgSearchService.cleanDetect(sessionKey, dataKey);
         }
      }).sendRequest(new Object[0]);
   }

   @RequestMapping(
      value = {"/user/auth"},
      method = {RequestMethod.POST}
   )
   public JSONObject userAuth(@RequestParam("username") String userName, @RequestParam("password") String password) {
      return this.imgSearchService.userAuth(userName, password);
   }

   @RequestMapping(
      value = {"/checkFace"},
      method = {RequestMethod.POST}
   )
   public JSONObject checkFace(@RequestParam("image") File image, @RequestParam("commandType") String commandType, @RequestParam("detectType") String detectType, @RequestParam("userToken") String userToken) {
      return this.imgSearchService.checkFace(image, commandType, detectType, userToken);
   }

   @RequestMapping(
      value = {"/detectFace"},
      method = {RequestMethod.POST}
   )
   public JSONObject detectFace(@RequestBody PortraitCondition portraitCondition) {
      return this.imgSearchService.detectFace(portraitCondition);
   }

   @RequestMapping(
      value = {"/portraitImageSearch"},
      method = {RequestMethod.POST}
   )
   public JSONObject portraitImageSearch(@RequestBody PortraitSearchCondition portraitSearchConditions) {
      return this.imgSearchService.portraitImageSearch(portraitSearchConditions);
   }
}
